package tech.aistar.day09;

import java.util.*;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2022/7/28 4:15 下午
 */
public class MapEntryTest {
    public static void main(String[] args) {
        Map<Integer,String> maps = new HashMap<>();

        maps.put(1,"java");
        maps.put(2,"python");
        maps.put(3,"mysql");

        Set<MyEntry<Integer,String>> sets = myEntrySet(maps);

        Iterator<MyEntry<Integer,String>> iter = sets.iterator();
        while(iter.hasNext()){
            MyEntry<Integer,String> e = iter.next();
            Integer key = e.getKey();
            String value = e.getValue();
            System.out.println(key+":"+value);
        }

    }

    private static Set<MyEntry<Integer,String>> myEntrySet(Map maps){
        Set<MyEntry<Integer,String>> set = new HashSet<>();

        //{1=java, 2=boot, 3=mysql}
        String str = maps.toString();
        System.out.println(str);
        str = str.substring(1,str.length()-1);
        //System.out.println(str);
        String[] arr = str.split(",");
        for (String s : arr) {
           String item = s.trim();
           //将每个key-value封装到了entry
           MyEntry<Integer,String> entry = new MyEntry<>();
           entry.setKey(Integer.valueOf(item.split("=")[0]));
           entry.setValue(item.split("=")[1]);

           set.add(entry);
        }


        return set;
    }
}
class MyEntry<K,V>{
    private K key;

    private V value;

    public MyEntry() {
    }

    public MyEntry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MyEntry{");
        sb.append("key=").append(key);
        sb.append(", value=").append(value);
        sb.append('}');
        return sb.toString();
    }
}